package com.example.sx_06.suxuantv.cookie;

import android.os.Handler;
import android.os.Looper;

import com.example.sx_06.suxuantv.App;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

public class OkManager {
    private OkHttpClient client;
    private volatile static OkManager okManager;
    private final String TAG = OkManager.class.getSimpleName();
    OkHttpClient.Builder httpBuilder;
    public OkManager() {
        //需要设置请求超时调用下面两行
        httpBuilder = new OkHttpClient.Builder();
        client = httpBuilder.readTimeout(10, TimeUnit.SECONDS)
                .connectTimeout(10, TimeUnit.SECONDS).writeTimeout(15, TimeUnit.SECONDS) //设置超时
                .cookieJar(new CookieJarImpl(new SPCookieStore(App.getApplication())))
                .build();
    }

    /**
     * 获取全局的cookie实例
     */
    public CookieJarImpl getCookieJar() {
        return (CookieJarImpl) client.cookieJar();
    }

    /**
     * 采用单例获取对象
     *
     * @return
     */
    public static OkManager getInstance() {
        if (okManager == null) {
            synchronized (OkManager.class) {
                if (okManager == null) {
                    okManager = new OkManager();
                }
            }
        }
        return okManager;
    }
}